<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      :request-api="getCustomGroupList"
      :init-param="initParam"
      :request-auto="true"
      :data-callback="dataCallback"
      :search-col="{ xs: 1, sm: 1, md: 1, lg: 1, xl: 1 }"
      row-key="attrGroupId"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" :icon="CirclePlus" @click="add">新增</el-button>
        <el-button type="primary" :icon="EditPen" plain @click="edit(scope)">编辑</el-button>
        <el-button type="primary" :icon="Delete" plain @click="del(scope)">删除</el-button>
        <!-- <el-button type="primary" :icon="Search" plain @click="search(scope)">搜索</el-button> -->
      </template>
    </ProTable>

    <el-drawer v-model="drawer" :title="`${title}自定义字段组`" :direction="direction" :before-close="handleClose">
      <el-form>
        <el-form-item label="名称">
          <el-input v-model="groupName" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="排序">
          <el-input v-model="groupSort" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="操作">
          <el-button type="primary" :icon="Upload" @click="UploadGroup">提交</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script lang="ts" setup name="customFieldGouping">
import { ref, reactive, onMounted } from "vue";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps, HeaderRenderScope } from "@/components/ProTable/interface";
import { getCustomGroupList, addCustomGroup, deleteCustomGroup, editCustomGroup } from "@/api/modules/dataDictionary";
import { CirclePlus, Delete, EditPen, Search, Download, Upload, View, Refresh, InfoFilled } from "@element-plus/icons-vue";
import { FormProps, ElMessage } from "element-plus";
import type { DrawerProps } from "element-plus";
const proTable = ref<ProTableInstance>();
const columns = reactive<ColumnProps<any>[]>([
  { type: "selection" },
  {
    prop: "attrGroupName",
    label: "名称",
    sortable: true,
    search: {
      el: "input",
      tooltip: "输入自定义字段名称",
      label: "输入名称",
      key: "attrGroupQueryDTO.attrGroupName"
    }
  },
  { prop: "attrGroupSortNo", label: "排序", sortable: true }
]);
// onMounted(() => {});
const initParam = ref("");
const drawer = ref(false);
const title = ref("");
const direction = ref<DrawerProps["direction"]>("rtl");
const groupName = ref("");
const groupSort = ref("");
const groupId = ref("");
const dataCallback = (data: any) => {
  if (data.data != null) {
    data.data.forEach((child: any) => {
      if (child.createTime) {
        child.createTime = child.createTime.replace("T", " ");
      }
      if (child.updateTime) {
        child.updateTime = child.updateTime.replace("T", " ");
      }
    });
  }
  return {
    list: data.data,
    total: data.totalSize,
    pageNum: data.page,
    pageSize: data.rows
  };
};
const handleClose = () => {
  //
  drawer.value = false;
};
//添加
const add = async () => {
  drawer.value = true;
  title.value = "新增";
};
const UploadGroup = async scope => {
  if (title.value === "新增") {
    const params = {
      "attributeGroupDTO.attrGroupName": groupName.value,
      "attributeGroupDTO.attrGroupSortNo": groupSort.value
    };
    if (groupName.value === "") {
      ElMessage.error("请输入名称");
    } else if (groupSort.value === "") {
      ElMessage.error("请输入排序");
    } else {
      const data = await addCustomGroup(params);
      if (data == null) {
        ElMessage.success(title.value + "成功");
        drawer.value = false;
        groupName.value = "";
        groupSort.value = "";
        proTable.value?.getTableList();
      }
    }
  } else if (title.value === "编辑") {
    const params = {
      "attributeGroupDTO.attrGroupId": groupId.value,
      "attributeGroupDTO.attrGroupName": groupName.value,
      "attributeGroupDTO.attrGroupSortNo": groupSort.value
    };
    const data = await editCustomGroup(params);
    console.log(data);
    if (data == null) {
      ElMessage.success(title.value + "成功");
      drawer.value = false;
      groupName.value = "";
      groupSort.value = "";
      proTable.value?.getTableList();
    }
  }
};
//删除
const del = async scope => {
  //
  if (scope.selectedList.length === 0) {
    ElMessage.error("请选择要删除的数据");
    return;
  } else {
    try {
      let params = "";
      scope.selectedList.forEach(item => {
        console.log(item.attrGroupId);
        params += "ids=" + item.attrGroupId + "&";
      });
      const data = await deleteCustomGroup(params);
      if (data == null) {
        ElMessage.success("删除成功");
        proTable.value?.clearSelection();
        proTable.value?.getTableList();

      }
    } catch {
      ElMessage.error("此数据存在相关联的数据，不能删除");
    }
  }
};
//编辑
const edit = e => {
  if (e.selectedList.length > 1 || e.selectedList.length === 0) {
    ElMessage.error("请选择一条数据");
    return;
  } else {
    drawer.value = true;
    title.value = "编辑";
    groupName.value = e.selectedList[0].attrGroupName;
    groupSort.value = e.selectedList[0].attrGroupSortNo;
    groupId.value = e.selectedList[0].attrGroupId;
  }
};
</script>
